<?php
class koudai_orderControl extends skymvc{
	public $user;
	public $userid;
	public $koudai_order_type=1;
	public $oc_where;
	public function __construct(){
		parent::__construct();	
	}
	
	public function onInit(){
		$this->loadConfig("table");
		
		M("login")->checkLogin();
		$this->userid=M("login")->userid;
		$this->user=M("login")->getUser();
		
		$this->oc_where=M("login")->userid?" (userid=".M("login")->userid." or oc_ssid='".OC_SSID."')  ":"oc_ssid='".OC_SSID."' ";
		$this->oc_where.=" AND siteid=".SITEID;
	}
	
	public function onDefault(){
		
	}
	
	public function onConfirm(){
		 
		$user=$this->user;
		$shopid=get_post('shopid','i');
		$koudai=M("koudai")->selectRow("id=".$shopid);
		if(!$koudai){
			$this->goALl("夜猫店不存在",1);
		}
		$where=$this->oc_where." AND shopid=".$shopid;
		$koudai_orderCart=M('koudai_order_cart')->select(array("where"=>$where));
		if(empty($koudai_orderCart)) $this->goall("请先去选购产品",1,0,"/index.php");
		//收货地址
		foreach($koudai_orderCart as $v){				
			$object_ids[]=$v['object_id'];
			$productids[]=$v['productid'];
			$ksids[]=$v['ksid'];
		}
		if($koudai_orderCart){
				$products=M('product')->id_list(array("where"=>" id in("._implode($productids).") "));
				$kslist=M('product_ks')->id_list(array("where"=>"  id in("._implode($ksids).") "));
				$koudai_products=M('koudai_product')->id_list(array("where"=>" id in("._implode($object_ids).") "));
		}
		
		$total_money=0; 
		$total_num=0;
			foreach($koudai_orderCart as $k=>$v){
			 	$koudai_product=$koudai_products[$v['object_id']];
				$product=$products[$v['productid']]; 
				$v['title']=$product['title'];
				$v['imgurl']=images_site($product['imgurl']);
				$v['price']=$product['lower_price']>0?$product['lower_price']:$koudai_product['price'];
				$total_money+=$v['price']*$v['amount'];
				$total_num+=$v['amount'];
				$koudai_orderCart[$k]=$v;
			}
		$youka=M("youka")->get();
		
		$address=M("user_address")->select(array("where"=>"userid=".$user['userid']." AND status<10 AND siteid=".SITEID,"shop_order"=>"isdefault desc","limit"=>10));
		if($address){
			foreach($address as $v){
				$d_ids[]=$v['province_id'];
				$d_ids[]=$v['city_id'];
				$d_ids[]=$v['town_id'];
			}
			$dist_list=M("site_city")->id_title(array("where"=>" sc_id in(".implode(",",$d_ids).") ","start"=>0,"limit"=>1000000)); 
		}
		if($total_money<$koudai['qsjia']){
			$this->goALl("商品价格小于起送价".$koudai['qsjia']."元，不能下单哦",1);
		}
		$moneypay=0;	
		if($user['money']>=$total_money){
			$moneypay=1;
		}
		$this->smarty->goassign(array(
			"address"=>$address,
			"dist_list"=>$dist_list,
			"back_url"=>$_SERVER['HTTP_REFERER'],
			"koudai_orderCart"=>$koudai_orderCart,
			"koudai"=>$koudai,
			"total_money"=>$total_money,
			"user"=>$user,
			"total_num"=>$total_num,
			"youka"=>$youka,
			"moneypay"=>$moneypay, 
			"ssyouka"=>M("youka")->get(M("login")->userid,2),
			"pay_type_list"=>pay_type_list($moneypay),
		));
		$this->smarty->display("koudai_order/confirm.html");
		
	}
	

	
	public function onorder(){
		
		$user=$this->user;
		$shopid=post('shopid','i');
		$user_address_id=post('user_address_id','i');
		$addr=M("user_address")->selectRow(array("where"=>"id=".$user_address_id));
		if(empty($addr) or empty($addr['telephone'])){
			$this->goall("请完善收货地址",1);
		}
		//处理商品
		$ids=post('cart_id','i');
		if($ids){
			$cart_list=M("koudai_order_cart")->select(array("where"=> $this->oc_where ." AND id in("._implode($ids).") "));
			 
			if(empty($cart_list)) $this->goall("请选择要购买的商品",1);
			$total_money=0;
			$koudais=array();
			foreach($cart_list as $v){				
				$object_ids[]=$v['object_id'];
				$ksids[]=$v['ksid'];
				$productids[]=$v['productid'];
			}
			
			if($cart_list){
				$products=M('product')->id_list(array("where"=>" id in("._implode($productids).") "));
				$kslist=M('product_ks')->id_list(array("where"=>"  id in("._implode($ksids).") "));
				$koudai_products=M('koudai_product')->id_list(array("where"=>" id in("._implode($object_ids).") "));
			}
			 
			foreach($cart_list as $k=>$v){
				$product=$products[$v['productid']];
				$koudai_product=$koudai_products[$v['object_id']];
				if(empty($product) or ($product['status']!=2)){
					$this->goall("产品".$product['title']."已下线，请删除该商品",1);
				}
				if($koudai_product['total_num']<$v['amount']){
					$this->goall("产品<a href='".R("/index.php?m=show&id=".$product['id'])."'>".$product['title']."</a>库存不足，当前还剩".($koudai_product['total_num'])."件，请删除该商品",1);
				}
				$v['price']=$product['lower_price']>0?$product['lower_price']:$koudai_product['price'];
				$total_money +=$v['price']*$v['amount'];
				if($v['lower_price']>0){
					$discount_money+=($product['price']-$product['lower_price'])*$v['amount'];
				}
				$cart_list[$k]=$v;
			}
			
			
			
		}else{
			$this->goall("请选择要购买的商品",1);
		}
		
		//End 处理商品
		$koudai=M("koudai")->selectRow("id=".$shopid);
		if($total_money<$koudai['qsjia']){
			$this->goALl("商品价格小于起送价".$koudai['qsjia']."元，不能下单哦");
		}
		$isfinish=0;
		$ispay=1;
		$this->loadControl("jfapi");
		$goods_money=$total_money;
		$express_money=EXPRESS_FEE;
		$total_money=$total_money+$express_money;
		$fenqi=get_post('fenqi','i');
		$youka=M("youka")->get(M("login")->userid,$fenqi);
		
		if($fenqi && ($youka['money']<$total_money)){
			$this->goAll("您的授信额度不足，请选择其他付款方式",22);
		}
		/**判断是否存在未结算订单**/
		 
		if($fenqi){
			if($fenqi==2){
				$sushe=M("sushe_user")->selectRow(array(
					"fields"=>"sushe",
					"where"=>" userid=".M("login")->userid
				));
				
				if(empty($sushe)){
					$this->goAll("您还未加入宿舍哦",1);
				}
				$checkorder=M("koudai_order")->selectRow("status<4 AND (isdz=0 or ispay=1)   AND susheid=".$sushe['sushe']);
				if($checkorder){
					$this->goAll("你还有未对账订单哦，赶紧去对账一下",1);
				}
				
			}else{
				$checkorder=M("koudai_order")->selectRow("status<4 AND (isdz=0 or ispay=1)  AND userid=".M("login")->userid);
				if($checkorder){
					$this->goAll("你还有未对账订单哦，赶紧去对账一下",1);
				}
			}
		}
		/* Start优惠券 */
		M("koudai_order")->begin();
		$coupon_id=post('coupon_id','i');
		if($coupon_id){
			 
			$coupon=M("coupon")->selectRow("id=".$coupon_id." AND siteid=".SITEID);
			if(!in_array($coupon['type_id'],array(1,3)) or $goods_money<$coupon['lower_money']){
				M("koudai_order")->rollback();
				$this->goAll("当前优惠券不能使用",1,$coupon);
			}
			$coupon_user=M("coupon_user")->selectRow("coupon_id=".$coupon_id." AND status=0 AND userid=".M("login")->userid);			
			if($coupon && $coupon_user){
				$coupon_money=$coupon['money'];
				M("coupon_user")->update(array("status"=>1),"id=".$coupon_user['id']);
				M("coupon")->update(array("use_num"=>$coupon['use_num']+1),"id=".$coupon_id);
				$total_money=$total_money-$coupon_money;
			}else{
				$coupon_id=0;
			}						
		}else{
			$coupon_money=0;
		}
		/* End 优惠券*/
		if($fenqi){
			/*处理分期付款*/
			M("youka")->addMoney(array(
					"userid"=>M("login")->userid,
					"typeid"=>$fenqi,
					"money"=>-$total_money,
					"content"=>"您购买了产品花了[money]元信用额度",			
			));
				 
			 $ispay=2;
			 $isfinish=1;
		}else{
			//处理余额消费
			$pay_type=get_post('pay_type','h');
			if($pay_type=='moneypay'){
				if($total_money>$user['money']){
					$this->goALL("余额不足",1);
				}else{
					//添加金钱消费记录		
					M("user")->addMoney(array(
						"userid"=>M("login")->userid,
						"money"=>-$total_money,
						"content"=>"您购买了产品花了[money]元",	
					));
					$ispay=2;
					$isfinish=1;
				}
			}
			
			if($pay_type=='unpay'){
				$isfinish=1;
				$unpay=1;
			}
		}
			//处理订单
			$last_ykmoney=0;
			if($fenqi){
				$lastfq=M("koudai_order")->getGzProduct($user['userid'],$fenqi);
				$oids=$lastfq['oids']; 
				$gzproducts=$lastfq['products'];
				$last_ykmoney=$lastfq['money'];
				if($lastfq){
					M("koudai_order")->update(array(
						"isadd"=>1
					),"order_id in("._implode($oids).")");
				}
			}
			$orderno="kd".date("YmdHis").$user['userid'];
			$comment=post('comment','h');
			$order_id=M("koudai_order")->insert(array(
				"orderno"=>$orderno,
				"dateline"=>time(),
				"ispay"=>$ispay,
				"userid"=>$user['userid'],
				"send_id"=>post('send_id','i'),
				"comment"=>$comment,
				"money"=>$total_money,
				"express_money"=>$express_money,
				"goods_money"=>$goods_money,
				"discount_money"=>$discount_money,
				"user_address_id"=>$user_address_id,
				"object_id"=>0,
				"unpay"=>$unpay,
				"shopid"=>$shopid,
				"coupon_id"=>$coupon_id,
				"coupon_money"=>$coupon_money,
				"sushe"=>$addr['sushe'],
				"sc_id"=>$addr['city_id'],
				"fenqi"=>$fenqi,
				"susheid"=>intval($youka['sushe']),
				"last_ykmoney"=>$last_ykmoney,
				"youka"=>intval($youka['id']),
				"siteid"=>SITEID,
				"paytype"=>$pay_type,
				"lastorders"=>base64_encode(json_encode($oids)),
			)); 
			//处理宿舍成员关联
			if($fenqi==2){
				$sscy=M("sushe")->getcyByUser(M("login")->userid);
				if($sscy){
					foreach($sscy as $v){
						M("koudai_order_ssuser")->insert(array(
							"order_id"=>$order_id,
							"userid"=>$v['userid']
						));
					}
				}
			}
			
			//处理上期压款订单
			
			//处理订单的产品
			$prostr="\n\r";
			if($order_id){
				
				foreach($cart_list as $k=>$v){
					$prostr.= $products[$v['productid']]['title']."x".$v['amount'].", \n\r ";
					if(isset($gzproducts[$v['productid']])){
						$amount=$v['amount']+$gzproducts[$v['productid']]['gznum'];
						unset($gzproducts[$v['productid']]);
					}else{
						$amount=$v['amount'];
						
					}
					//打印产品信息
					$printer_products[]=array(
						"title"=>$products[$v['productid']]['title'],
						"price"=>$v['price'],
						"amount"=>$v['amount'],
						"money"=>$v['price']*$v['amount'],
					);
					M("koudai_order_product")->insert(array(
						"order_id"=>$order_id,
						"userid"=>$user['userid'],
						"object_id"=>$v['object_id'],
						"productid"=>$v['productid'],
						"type_id"=>$v['type_id'],
						"price"=>$v['price'],
						"amount"=>$amount,
						"amount_send"=>$v['amount'],
						"dateline"=>time(),
						"ksid"=>$v['ksid'],
						 
					));
					//减掉商品库存
					M("koudai_product")->total_num_add(array(
						"shopid"=>$v['shopid'],
						"productid"=>$v['productid'],
						"total_num"=>-$v['amount'],
						"content"=>"您下了单产品[title]库存减少[total_num]"
					));
					
				}
				
				if(!empty($gzproducts)){
					foreach($gzproducts as $v){
						M("koudai_order_product")->insert(array(
							"order_id"=>$order_id,
							"userid"=>$user['userid'],
							"object_id"=>$v['object_id'],
							"productid"=>$v['productid'],
							"type_id"=>$v['type_id'],
							"price"=>$v['price'],
							"amount"=>$v['gznum'],
							"gzproduct"=>1,
							"dateline"=>time(),

							 
						));
					}
				} 
				
			}else{
				M("koudai_order")->rollback();
				$this->goall("订单处理失败",1,0,$back_url);
			}
			
			//处理订单收货地址
			$d_ids=array($addr['province_id'],$addr['city_id'],$addr['town_id']);
			$dist_list=M("site_city")->id_title(array("where"=>" sc_id in(".implode(",",$d_ids).") ","start"=>0,"limit"=>1000000)); 
			M("koudai_order_address")->insert(array(
				"order_id"=>$order_id,
				"userid"=>$user['userid'],
				"truename"=>$addr['truename'],
				"telephone"=>$addr['telephone'],
				"p_c_t"=>$dist_list[$addr['province_id']].$dist_list[$addr['city_id']].$dist_list[$addr['town_id']],
				"address"=>$addr['address'],
				"dateline"=>time(),
				"sushe"=>$addr['sushe']
			));
		 //打印收货地址
		 
			if($shop['sendall']){
				$address=$addr['address'];
			}else{
				$address=$dist_list[$addr['province_id']].$dist_list[$addr['city_id']].$dist_list[$addr['town_id']];
			}
			$site=M("sites")->selectRow("siteid=".SITEID);
			$printer_address=array(
				"address"=>$site['sitename']." ".$address."宿舍".$addr['sushe'],
				"telephone"=>$addr['telephone'],
				"nickname"=>$addr['truename']
			);
		//清除购物车
		$prostr.=$addr['truename'].",".$addr['telephone'].",".$dist_list[$addr['province_id']].$dist_list[$addr['city_id']].$dist_list[$addr['town_id']].",宿舍".$addr['sushe'];
		M("koudai_order_cart")->delete($this->oc_where." AND id in("._implode($ids).")");
		//发送推送信息
		$content=date("H:i:s")."您的口袋店有新的订单了，详情：".$prostr;
		M("apppush")->sendShop(array(
				"table"=>"koudai",
				"content"=>$content,
				"shopid"=>$shopid,
				"retable"=>"koudai"	
			));
		//End 推送信息
		
		//手机短信
		if(SMS_KOUDAI_ORDER_NEW){
			$smsdata=array(
				"key"=>"order",
				"para"=>array($orderno."(校园超市 ".date("Y-m-d H:i:s")."详情:".$prostr.")","",""),
				"content"=>"【".SMS_QIANMING."】".$content
			);
			$koudai_admins=M("koudai_admin")->select(array("where"=>" shopid=".$shopid));
			if($koudai_admins){
				 
				foreach($koudai_admins as $y){
					if(is_tel($y['telephone'])){
						M("smspush")->insert(array(
							"telephone"=>$y['telephone'],
							"dateline"=>time(),
							"content"=>base64_encode(serialize($smsdata))
						));
					}
				}
			}
		}
		
		M("koudai_order")->commit();
		$data=array(
			"order_id"=>$order_id,
			"pay_type"=>$pay_type,
			"ispay"=>$ispay
		);
		/****打印服务****/
		if(HAS_PRINTER==1){ 
			if($isfinish){
				$printer_isvalid=1;
			}else{
				$printer_isvalid=0;
			}
			$pays=pay_type_list(1);
			$printer_order=array(
				"money"=>$total_money,
				"dateline"=>time(),
				"pay_type"=>$pays[$pay_type],
				"orderno"=>$orderno,
				"comment"=>$comment
			);
			$printer=M("mod_printer")->selectRow("tablename='koudai' AND bstatus=2 AND shopid=".$shopid);
			$printer_content=array(
				"products"=>$printer_products,
				"addr"=>$printer_address,
				"order"=>$printer_order,
			);
			$printer_content=base64_encode(json_encode($printer_content));
			if($printer ){
				M("mod_printer_plan")->insert(array(
					"tablename"=>"koudai",
					"shopid"=>$shopid,
					"title"=>"订单".$orderno,
					"dateline"=>time(),
					"last_time"=>time(),
					"ftable"=>"koudai_order",
					"fid"=>$order_id,
					"content"=>$printer_content,
					"isvalid"=>$printer_isvalid
				));
			}
		}
			/*****End打印服务**********/
		if($isfinish){
			$this->goall($this->lang['koudai_order_success'],0,$data,"/index.php?m=koudai_order&a=show&order_id=".$order_id);
		}else{
			//header("Location: /index.php?m=recharge&a=default&table=koudai&pay_type=".$pay_type."&order_id=".$order_id);
			$this->goall("下单成功",0,$data,"/index.php?m=koudai_order&a=pay&pay_type=".$pay_type."&order_id=".$order_id);
		}
	}
	
	public function onPay(){
		$order_id=get('order_id','i');
		$pay_type=get('pay_type','h');
		header("Location:  /index.php?m=recharge&a=default&table=koudai&pay_type=".$pay_type."&order_id=".$order_id);
		exit();
		$this->smarty->assign(array(
			"payurl"=>"/index.php?m=recharge&a=default&table=koudai&pay_type=".$pay_type."&order_id=".$order_id,
			"order"=>M("koudai_order")->selectRow("order_id=".$order_id)
		));
		$this->smarty->display("koudai_order/pay.html");
	}
	 
	 
	public function onMy(){
		 
		//$where=" siteid=".SITEID." AND (isadd=0 or(isadd=1 AND isdz=1) )  AND status<8 AND userid=".$this->userid;
		$where=" siteid=".SITEID." AND status<11 AND userid=".$this->userid;
		$url=APPINDEX."?m=koudai_order&a=my";
		$start=get('per_page','i');
		$limit=20;
		$status=get('status','i');
		if($status){
			$where.=" AND status=".($status-1);
			$url.="&status=".$status;
		}
		$option=array(
			"where"=>$where,
			"order"=>"order_id DESC",
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("koudai_order")->select($option,$rscount);
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		if($data){
			foreach($data as $k=>$v){
				$v['address']=M("koudai_order_address")->selectRow(array("where"=>"order_id=".$v['order_id'],"koudai_order"=>"id DESC"));
			 
				$v['koudai_product']=$this->orderproduct($v['order_id']);
				$amount=0;
				 foreach($v['koudai_product'] as $ppp){
					$amount+=$ppp['amount']; 
				 }
				 $v['num']=$amount;
				 $v['status_name']=$order_status_list[$v['status']];
				if($v['paytype']!='unpay'){
					$v['ispay_name']=$order_ispay[$v['ispay']];
				}else{
					$v['ispay_name']="货到付款";
				}
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		
		$this->smarty->goassign(array(
			"data"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			
		));
		$this->smarty->display("koudai_order/my.html");
	}
	

	
	public function onshow(){
		 
		$order_id=get('order_id','i');
		$data=M("koudai_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		$koudai=M("koudai")->selectRow("id=".$data['shopid']);
		if($data['fenqi']==2){
			$sscy=M("koudai_order_ssuser")->selectRow("userid=".M("login")->userid." AND order_id=".$order_id."");
			 
			if(!$sscy){
				$this->goall("该订单不是您的",1);
			}

		}else{
			if($data['userid']!=M("login")->userid ){
				$this->goall("该订单不是您的",1);
			}
		}
		$addr=M("koudai_order_address")->selectRow(array("where"=>"order_id=".$order_id,"koudai_order"=>"id DESC"));
		 
		$koudai_order_status_list=$this->config_item('order_status_list');
		
		$koudai_order_type_list=$this->config_item('order_type_list');
		$koudai_order_ispay=$this->config_item('order_ispay');
		//获取商品
		 
		$koudai_order_product=M('koudai_order_product')->select(array("where"=>"order_id=".$data['order_id']." AND gzproduct=0"));
		 
		foreach($koudai_order_product as $k=>$v){
			$p=M('product')->selectRow(array("where"=>"id=".$v['productid']));
			$p['order_price']=$v['price'];
			$p['iscomment']=$v['iscomment'];
			$p['rating_grade']=$v['rating_grade'];
			$p['amount']=$v['amount'];
			$p['amount_send']=$v['amount_send'];
			$ks=M('product_ks')->selectRow("id=".$v['ksid']);
			if($ks){
				$p['ks_title']=$ks['title'];
				$p['price']=$ks['price'];
			}
			$koudai_order_product[$k]=$p;
		}
		 
		$this->smarty->goassign(array(
			"data"=>$data,
			"addr"=>$addr,
			"koudai"=>$koudai, 
			"order_status_list"=>$koudai_order_status_list,
			"order_type_list"=>$koudai_order_type_list,
			"order_ispay"=>$koudai_order_ispay,
			"order_product"=>$koudai_order_product,
		));
		$this->smarty->display("koudai_order/show.html");
	}
	
	public function orderproduct($order_id){
		
		$order_product=M("koudai_order_product")->select(array("where"=>"order_id=".intval($order_id)));
		if($order_product){
			foreach($order_product as $k=>$v){
				$productids[]=$v['productid'];
			}
			$products=M("product")->getListByIds($productids);
			foreach($order_product as $k=>$v){
				$p=$products[$v['productid']];
				$p['order_price']=$v['price'];
				$p['amount']=$v['amount'];
				$p['total_money']=$v['amount']*$v['price'];
				$p['gznum']=$v['gznum'];
				$p['thnum']=$v['thnum'];
				$p['jsnum']=$p['amount']-$v['gznum']-$v['thnum'];
				$p['order_product_id']=$v['id'];
				$order_product[$k]=$p;
			}
		}
		return $order_product;	
	}
	
	public function onReceive(){
		$order_id=get('order_id','i');
		$data=M("koudai_order")->selectRow(array("where"=>" order_id=".$order_id."  "));
		if(empty($data) or $data['status']<2){
				$this->goall($this->lang['data_no_exists'],1,0,"/index.php");
			}
		if($data['fenqi']==2){
			$sscy=M("koudai_order_ssuser")->selectRow("userid=".M("login")->userid." AND order_id=".$order_id."");
			 
			if(!$sscy){
				$this->goall("该订单不是您的",1);
			}
		}else{
			if($data['userid']!=M("login")->userid){
				$this->goall("您无权限",1,0,"/index.php");
			}
		}
		M("koudai_order")->update(array("isreceived"=>2)," order_id=".$order_id." ");
		$this->goall($this->lang['save_success'],0,$data);
	}
	
	public function oncomment(){
		$order_id=get_post('order_id','i');
		$id=get_post('id','id');
		$order=M("koudai_order")->selectRow("order_id=".$order_id);
		if($order['userid']!=M("login")->userid or $order['status']!=3 ){
			$this->goall ("订单信息出错",1);
		}
		$shop=M("koudai")->selectRow("id=".$order['shopid']);
		$comment=M("koudai_order_comment")->selectRow("order_id=".$order_id);
		$this->smarty->assign(array(
			"order"=>$order,
			"shop"=>$shop,
			"comment"=>$comment
		)); 
		$this->smarty->display("koudai_order/comment.html");
	}
	
	
	
	public function onCommentSave(){
		$order_id=get_post('order_id','i');
		$id=get_post('id','id');
		$order=M("koudai_order")->selectRow("order_id=".$order_id);
		if($order['status']!=3 ){
			$this->goall ("订单信息出错",1);
		}
		if($data['fenqi']==2){
			$sscy=M("koudai_order_ssuser")->selectRow("userid=".M("login")->userid." AND order_id=".$order_id."");
			 
			if(!$sscy){
				$this->goall("该订单不是您的",1);
			}
		}else{
			if($data['userid']!=M("login")->userid){
				$this->goall("您无权限",1,0,"/index.php");
			}
		}
		$comment=M("koudai_order_comment")->selectRow("order_id=".$order_id);
		$data=M("koudai_order_comment")->postData();
		$data['siteid']=SITEID;
		$data['shopid']=$order['shopid'];
		$data['dateline']=time();
		$data['userid']=M("login")->userid;
		if($comment){
			M("koudai_order_comment")->update($data,"id=".$comment['id']);
		}else{
			M("koudai_order_comment")->insert($data);
		}
		$this->goAll("评价成功",1);		
	}
	
	public function onproduct_comment(){
		$order_id=get_post('order_id','i');
		$id=get_post('id','id');
		$koudai_order=M("koudai_order")->selectRow("order_id=".$order_id);
		if($koudai_order['userid']!=$this->userid or $koudai_order['status']!=3 ){
			$this->goall ("订单信息出错",1);
		}
		$row=M("koudai_order_product")->selectRow(" order_id=".$order_id." AND object_id=".$id." ");
		if(empty($row) or $row['iscomment'] ){
			$this->goall("产品出错",1);
		}
		$this->smarty->display("koudai_order/koudai_product_comment.html");
	}
	
	public function onproduct_comment_save(){
		$order_id=get_post('order_id','i');
		$id=get_post('id','id');
		$koudai_order=M("koudai_order")->selectRow("order_id=".$order_id);
		if($koudai_order['userid']!=$this->userid or $koudai_order['status']!=3 ){
			$this->goall("订单信息出错",1);
		}
		$row=M("koudai_order_product")->selectRow(" order_id=".$order_id." AND object_id=".$id." ");

		if(empty($row) or $row['iscomment']){
			$this->goall("产品出错",1);
		}
		 
		$data=array(
			"grade"=>post('grade','i'),
			"userid"=>$this->userid,
			"object_id"=>$id,
			"type_id"=>1,
			"dateline"=>time(),
			"status"=>0,
			"model_id"=>MODEL_koudai_PRODUCT_ID,
			"content"=>post('content','h'),
			"order_id"=>$order_id
		);
		M("rating")->insert($data);
		M("koudai_order_product")->update(array("iscomment"=>1,"rating_grade"=>post('grade','i'))," order_id=".$order_id." AND object_id=".$id." ");
		$pro=M("koudai_product")->selectRow("id=".$id);
		$rating_grade=M("rating")->selectOne(array("where"=>" model_id=".MODEL_koudai_product_ID." AND type_id=1 AND object_id=".$id." ","fields"=>" AVG(grade) as g"));
		M("koudai_product")->update(array("rating_grade"=>$rating_grade,"rating_num"=>$pro['rating_num']+1)," id=".$id);
		$this->goall("评论成功");
	}
	
	public function onCancel(){
		
		$order_id=get_post('order_id','i');
		$data=M("koudai_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if($data['status']>=3) $this->goall("操作失败",1);
		if($data['fenqi']==2){
			$sscy=M("koudai_order_ssuser")->selectRow("userid=".M("login")->userid." AND order_id=".$order_id."");
			 
			if(!$sscy){
				$this->goall("该订单不是您的",1);
			}
		}else{
			if($data['userid']!=M("login")->userid){
				$this->goall("您无权限",1,0,"/index.php");
			}
		}
		M("koudai_order")->begin();
		M("koudai_order")->update(array("status"=>10,"whocancel"=>"user"),"order_id=".$order_id);
		$content=post('content');
		$message=post('message','h');
		M("koudai_order_log")->insert(array(
			"dateline"=>time(),
			"admin_id"=>$this->admin['id'],
			"order_id"=>$order_id,
			"content"=>$content."，原因：".$message,
		));
		/*取消上单对账添加的产品*/
		if($data['fenqi']){
			$lastorders=json_decode(base64_decode($data['lastorders']));
			if($lastorders){
				foreach($lastorders as $v){
					$lastorder=M("koudai_order")->update(array(
						"isadd"=>0
					),"order_id=".$v);
				}
			}
			
		}
		//加商品库存
		$order_product=M("koudai_order_product")->select(array("where"=>"order_id=".$order_id));
		foreach($order_product as $v){
			M("koudai_product")->total_num_add(array(
				"shopid"=>$data['shopid'],
				"productid"=>$v['productid'],
				"total_num"=>$v['amount_send'],
				"content"=>"[下单]"
			));
		}
	 	//通知商家
		$content=date("H:i:s")."您的订单".$data."被取消了。";
		M("apppush")->sendShop(array(
				"table"=>"koudai",
				"content"=>$content,
				"shopid"=>$data['shopid'],
		));
		//退钱给用户
		if($data['fenqi']){
			/*处理分期付款*/
			M("youka")->addMoney(array(
					"userid"=>M("login")->userid,
					"money"=>$data['money'],
					"typeid"=>$order['fenqi'],
					"youka"=>$order['youka'],
					"content"=>"您的订单取消了，信用余额加了[money]元",			
			));
		}elseif($data['ispay']==2){
			/*
			$row=M("recharge_back")->selectRow(" order_id=".$order_id." AND tablename='koudai' ");
			if(empty($row)){
				M("recharge_back")->insert(array(
					"order_id"=>$order_id,
					"tablename"=>"koudai",
					"paytype"=>$data['paytype'],
					"dateline"=>time(),
					"ip"=>ip(),
					"userid"=>M("login")->userid	
				));
			}*/
			 			
			$option=array(
					"userid"=>$data['userid'],
					"money"=>$data['money'],
					"content"=>"您的订单被取消了，退还给您￥".$data['money']."",
					"siteid"=>SITEID
			);
			M("user")->addMoney($option);
			 
		}
		M("koudai_order")->commit();
		$this->goall("取消成功");
	}
	
}
?>